import 'package:dio/dio.dart';
import 'package:dio/native_imp.dart';
import 'dart:convert' as convert;

import 'package:flutter/foundation.dart';

enum MethodType { GET, POST, PATCH, DELETE}

// 必须是顶层函数
_parseAndDecode(String response) {
  return convert.jsonDecode(response);
}

parseJson(String text) {
  return compute(_parseAndDecode, text);
}

abstract class BaseHttp extends DioForNative {
  BaseHttp({Key? key}) {
    /// 初始化 加入app通用处理
    (transformer as DefaultTransformer).jsonDecodeCallback = parseJson;
    interceptors.add(HeaderInterceptor());
    init();
  }

  void init();
}

/// 添加常用拦截器
class HeaderInterceptor extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    options.connectTimeout = 1000 * 30000;
    options.receiveTimeout = 1000 * 30000;
    options.sendTimeout =    1000 * 30000;
    return handler.next(options);
  }
}

/// 子类需要重写
abstract class BaseResponseData {
  int? code = 200;
  String? msg;
  dynamic data;

  bool get success;

  BaseResponseData({this.code, this.msg, this.data});

  @override
  String toString() {
    return 'BaseRespData{code: $code, message: $msg, data: $data}';
  }
}